﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;

namespace Gollie.Ajax
{
	public class NewPassword : AjaxResult
	{
		public NewPassword() : base(null) { }

		public NewPassword(AjaxArg arg)
			: base(arg)
		{
		}

		protected override void Init(AjaxArg ajaxArg)
		{
			NewPasswordArg arg = (NewPasswordArg)ajaxArg;

			logger.Info("Admin userId {0} resetting password for user {1}...", arg.tokenUser, arg.username);

			try
			{
				string ww = arg.ww.Trim();
				UserInfo.ValidateWW(ww);

				/* Validation success! */
				string wwHash = Hash.GetHash(ww);

				Database.Queries.NewPassword(arg.username, Hash.GetHash(ww));
			}
			catch (MySqlException mySqlEx)
			{
				this.ErrorCode = mySqlEx.Number;
				this.ErrorMessage = MySqlErrorCodes.GetInstance().GetMessage(mySqlEx);
				logger.Info("Reset vendoruser password failed: {0}", this.ErrorMessage);
			}
			catch (Exception e)
			{
				if (this.ErrorCode == 0)
					this.ErrorCode = 99;

				this.ErrorMessage = e.Message;
				logger.Info("Reset vendoruser password failed: {0}", this.ErrorMessage);
			}
		}
	}
}